//
//  EditGuestInfoController.m
//  QRMaster
//
//  Created by myApple on 16/4/28.
//  Copyright © 2016年 myApple. All rights reserved.
//

#import "EditGuestInfoController.h"
#import "RequestNetWork.h"
#import "EditGuestInfoCell.h"
#import "GuestLogic.h"
#import "GroupGuestLogic.h"


@interface EditGuestInfoController ()<UITableViewDataSource,UITableViewDelegate,EditGuestInfoCellDelegate>
{
    GuestModel * editModel;
    UITableView * editGuestTabelView;
    UIImageView * submitImageView;
    UILabel * submitLabel;
}

@end

@implementation EditGuestInfoController

-(id)initWithModel:(GuestModel *)model
{
    self = [super init];
    if (self) {
        editModel = model;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = NSLocalizedString(@"modifyGuestInfo", nil);
    self.view.backgroundColor = UIColorFromRGB(0xebebeb);
    [self prepareNavBtn];
    [self prepareTabelView];
    // Do any additional setup after loading the view.
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    submitImageView.hidden = NO;
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    submitImageView.hidden = YES;
}

-(void)prepareTabelView
{
    float x = 0;
    float y = 5;
    float height = UI_SCREEN_HEIGHT - 64 - y;
    float width = UI_SCREEN_WIDTH;
    
    editGuestTabelView = [[UITableView alloc]initWithFrame:CGRectMake(x, y, width, height) style:UITableViewStyleGrouped];
    editGuestTabelView.backgroundColor = UIColorFromRGB(0xebebeb);
    editGuestTabelView.delegate = self;
    editGuestTabelView.dataSource = self;
    editGuestTabelView.tableFooterView = [[UIView alloc]init];
    [self.view addSubview:editGuestTabelView];
}

-(void)prepareNavBtn
{
    submitImageView = [[UIImageView alloc]initWithFrame:CGRectMake(UI_SCREEN_WIDTH - 60, 2, 50, 40)];
    submitImageView.userInteractionEnabled = NO;
    UITapGestureRecognizer * submitTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(submit)];
    [submitImageView addGestureRecognizer:submitTap];
    [self.navigationController.navigationBar addSubview:submitImageView];
    
    submitLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 10, 50, 20)];
    submitLabel.text = NSLocalizedString(@"authenSubmit",@"完成");
    submitLabel.textColor = UIColorFromRGBAndA(0x7dc234, 0.5);
    submitLabel.font = [UIFont systemFontOfSize:15.0];
    [submitImageView addSubview:submitLabel];
}


#pragma mark ----------- 提交
-(void)submit
{
    [self.view endEditing:YES];
    if(editModel.guestName.length > 20 || editModel.guestName.length <1)
    {
        [PromptInfoView showMessage:NSLocalizedString(@"userNameLength", nil)];
        return;
    }
    NSMutableDictionary * saveDict = [[NSMutableDictionary alloc]init];
    [saveDict setValue:UID forKey:@"id"];
    [saveDict setValue:TOKEN forKey:@"token"];
    [saveDict setValue:editModel.guestid forKey:@"memberId"];
    [saveDict setValue:editModel.guestName forKey:@"name"];
    [saveDict setValue:editModel.sex forKey:@"gender"];
    [saveDict setValue:editModel.certificaties forKey:@"identityId"];
    [saveDict setValue:[QRSingleton sharedQRSingleton].communityId forKey:@"communityId"];
    
    if([self getTimestamp:[QRSingleton sharedQRSingleton].communityId timeKey:@"customer_timestamp"] != nil)
    {
        [saveDict setValue:[self getTimestamp:[QRSingleton sharedQRSingleton].communityId timeKey:@"customer_timestamp"] forKey:@"timestamp"];
    }
    else
    {
        [saveDict setValue:@"" forKey:@"timestamp"];
    }
    __weak EditGuestInfoController * weakSelf = self;
    RequestNetWork * rnw = [[RequestNetWork alloc]init];
    [rnw initWithPath:POST(EDITGUEST) params:saveDict block:^(id downLoadData, id error)
     {
         if([downLoadData objectForKey:@"cache"] != nil)
         {
             [PromptInfoView showMessage:NSLocalizedString(@"editSuccess", nil)];
             if([@"0" isEqualToString:[NSString stringWithFormat:@"%@",[[downLoadData objectForKey:@"cache"] objectForKey:@"status"]]]==YES)
             {
                 [self updateTimestamp:[QRSingleton sharedQRSingleton].communityId timeKey:@"customer_timestamp" timeKeyValue:[[downLoadData objectForKey:@"cache"] objectForKey:@"timestamp"]];
             }
             NSMutableDictionary *data = [downLoadData objectForKey:@"data"];
             if(data != nil)
             {
                 NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
                 [dic setObject:editModel.guestName forKey:@"name"];
                 [dic setObject:editModel.sex forKey:@"gender"];
                 [dic setObject:editModel.certificaties  forKey:@"identityId"];
                 [dic setObject:[self firstChar:editModel.guestName] forKey:@"firstChar"];
                 GuestLogic *guestLogic = [[GuestLogic alloc] init];
                 
                 if(_groupId)
                 {
                     GroupGuestLogic *groupGuest = [[GroupGuestLogic alloc]init];
                     [groupGuest updateGroupGuestInfo:editModel groupId:_groupId groupName:nil groupStartTime:nil groupEndTime:nil];
                 }
                 else
                 {
                     [guestLogic updateGuestInfo:editModel.guestid communityId:[QRSingleton sharedQRSingleton].communityId dic:dic];
                 }
             }
             if ([weakSelf editGuestBlock]) {
                 weakSelf.editGuestBlock(editModel);
             }
             [weakSelf.navigationController popViewControllerAnimated:YES];
         }
         else
         {
             [PromptInfoView showMessage:[error objectForKey:@"info"]];
         }
     }
     ];

}

#pragma mark --------tableviewdelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

//-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
//{
//    return NSLocalizedString(@"comments", nil);
//}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    static NSString *header = @"customHeader";
    UITableViewHeaderFooterView *vHeader;
    vHeader = [tableView dequeueReusableHeaderFooterViewWithIdentifier:header];
    
    if (!vHeader) {
        vHeader = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:header];
    }
    vHeader.textLabel.text = NSLocalizedString(@"comments", nil);
    return vHeader;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44.0;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 34.0;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString * identityId = @"cellId";
    EditGuestInfoCell * cell = [tableView dequeueReusableCellWithIdentifier:identityId];
    if (!cell) {
        cell = [[EditGuestInfoCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identityId];
    }
    
    cell.guestNameString = editModel.guestName;
    cell.delegate = self;
    return cell;
}

#pragma mark -------- cell delegate
-(void)canSave:(BOOL)saveFlag guestName:(NSString *)name
{
    editModel.guestName = name;
    submitImageView.userInteractionEnabled = saveFlag;
    if (saveFlag) {
        submitLabel.textColor = UIColorFromRGBAndA(0x7dc234, 1);
    }
    else
    {
        submitLabel.textColor = UIColorFromRGBAndA(0x7dc234, 0.5);
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
